require([
  "jquery",
  "ko",
  "bootstrap",
  "jquery-select2",
  "jquery-slimscroll",
  "jquery-gritter",
  "jquery-blockui",
  "bootstrap-datepicker",
  "bootstrap-datetimepickerlocal",
  "akcy"
], function ($, ko) {
  var akcy = arguments[arguments.length - 1];
  akcy.ui.init();

  var table = akcy.table().options({
    headers: [{
      text: '服务名称'
    }, {
      text: '抽取规则'
    }, {
      text: '任务编码'
    }, {
      text: '是否启动'
    }],
    // defaultOrderBy: "U_ID",
    dataQueryUrl: 'job/findAll'
  }).optionsExtend({
    appId: "auto",
    iconVal: ko.observable(),
    editInfo: function (data) {
      pageForm.load(data);
      akcy.showmodal("modal-user");
    },
    rowclick: function (rst) {
      pageForm.load(rst);
      var data = rst.cron.split(' ');
      rule.getInstance().checkedHour(data[2]);
      if (data[1].length > 1) {
        $("#hour").attr("checked", 'checked');
      } else {
        $("#defaulthour").attr("checked", 'checked');
      }
      rule.getInstance().checkedDay(data[3]);
      if (data[2].length > 1) {
        $("#day").attr("checked", 'checked');
      } else {
        $("#defaultday").attr("checked", 'checked');
      }
    },
    addService: function () {
      pageForm.getInstance().reset();
      akcy.showmodal("modal-user");
    },
    deleteInfo: function (data) {

      akcy.ajaxdelete('auto', 'job/deleteJobTrigger/' + data.serviceId).done(function () {
        akcy.alert.success("删除成功！");
        table.load();
      })

    },
    jobPause: function (data) {
      akcy.ajaxget("auto", 'job/jobPause/' + data.serviceId).done(function () {
        table.load();
      })
    },
    start: function (data) {
      akcy.ajaxget("auto", 'job/jobStart/' + data.serviceId).done(function () {
        table.load();
      })
    }
  }).bind("pagetree").load();
  var pageForm = akcy.form().options({
    fields: [{
      field: 'serviceId',
      value: ko.observable(-1)
    }, {
      field: 'jobName',
      value: ko.observable()
    }, {
      field: 'url',
      value: ko.observable()
    }, {
      field: 'cron',
      value: ko.observable()
    }, {
      field: 'serviceName',
      value: ko.observable()
    }, {
      field: 'isStart',
      value: ko.observable()
    }],
    dataAddUrl: 'job/addJobTrigger',
    dataUpdateUrl: 'job/modifyJobTrigger',
    dataDeleteUrl: ''
  }).optionsExtend({
    appId: "auto",
    added: function (rst, data) {
      if (data.success) {
        table.load();
        akcy.alert.success("新增成功");
        pageForm.getInstance().reset();
      } else {
        akcy.alert.info(data.message);
      }
    },
    beforeAdd: function () {
      pageForm.getInstance().cron($(".rule").val());
    },
    beforeUpdate: function () {
      pageForm.getInstance().cron($(".rule").val());
    },
    deleteInfo: function (data) {

      akcy.ajaxdelete('auto', 'job/deleteJobTrigger/' + pageForm.getInstance().serviceId()).done(function () {
        akcy.alert.success("删除成功！");
        table.load();
      })

    },
    updated: function (rst, data) {
      if (data.success) {
        table.load();
        akcy.alert.success("保存成功");
        pageForm.getInstance().reset();
      } else {
        akcy.alert.info(data.message);
      }
    },
    deleted: function () {
      akcy.alert.success("删除成功！");
    }
  }).bind("autoextractInfo");

  function changehourType() {
    $(".hour").click(function () {
      rule.getInstance().hourType($(this).val());
      // if ($(this).val() == 1)
      rule.getInstance().checkedHour([]);
    });
    $(".day").click(function () {
      rule.getInstance().dayType($(this).val());
      // if ($(this).val() == 1)
      rule.getInstance().checkedDay([]);
    });
  }
  changehourType();
  var rule = akcy.table().options({
    dataQueryUrl: ''



  }).optionsExtend({
    appId: "auto",
    hourData: ko.observableArray([
      0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20,
      21,
      22,
      23
    ]),
    dayData: ko.observableArray([
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20,
      21,
      22,
      23,
      24,
      25,
      26,
      27,
      28,
      29,
      30,
      31
    ]),
    checkedDay: ko.observableArray([]),
    hourType: ko.observable(1),
    dayType: ko.observable(1),
    checkedHour: ko.observableArray([]),
    changehourType: function (type) {
      rule.getInstance().hourType(type);
    }
  }).bind("rule");;
});